/**
*	企业路由处理函数
*	@A1uthor Apricity
**/
"use strict";

const { getToken } = require('../utils/jwtFun');

const urllib = require("urllib");

const { setCrypto, getCrypto } = require('../utils/crypto.js');

const cloudbase = require("@cloudbase/node-sdk");

const app = cloudbase.init({
    env: "work-xiangxiao-1gtlbaah05be1c75"
});

const db = app.database();

/**
 *  企业发送验证码
 */
const getMobileNote = async (req,res) => {
    try {
        
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
} 


/*
	企业注册	
*/
const businessRegister = async (req,res) => {
    try {
	let time = new Date();
    let password = setCrypto(req.password);
	let obj = {
		nickName: req.nickName,
		accounts: req.accounts,
		password: password,
		isDel: 0,
		time: time,
		info: req.info
	};
	const res = await db.collection("Business").add(obj);
	return {...res,status: 200,message: 'success'};
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
}

/**
 *  企业登陆
 */
const businessLogin = async (req,res) => {
    try {
        const {accounts,password} = req;
        const res = await db.collection("Business").where({accounts: accounts}).get();
        const oldPassword = res.data[0].password;
        const result = getCrypto(password,oldPassword);
        if (!result)  return {status: 400,message: '账号或密码不正确'};
        const token = getToken(accounts);
        const obj = {
            info: res.data[0].info,
            nickName: res.data[0].nickName
        }
        return {status: 200,message: '登陆成功',data: obj,token: token};
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
} 

/**
 * 修改企业个人资料
 */
const changeBusinessUserInfo = async (req,res) => {
    try {
        const {nickName, info, token } = req;
        const { accounts } = verifyToken(token);
        const result = await db.collection('Business').where({accounts: accounts}).update({nickName: nickName,info: info});
        return {status: 200,message: 'SUCCESS',accounts: accounts,data: {
            info: info, 
            nickName: nickName
        }};
    } catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
} 
/**
 * 企业发布工作
 */
const addWork =
 async (req,res) => {
    try {
        const {title , tag ,price,methods, workTime,workTime2,workLocation,workLimit, detail,contactMethods, workMethods, workMethods2, token } = req;
        const { accounts } = verifyToken(token);
        const result = await db.collection('Business').where({accounts: accounts}).get();
        let time = new Date();
        let obj = {
            ...req,
            selectPerson: 0,
            publicNickName: result.data[0].nickName,
            publicInfo: result.data[0].info,
            accounts : accounts,
            time : time,
            isDel : 0,
            workStatus : 1
        }
        const res = await db.collection("WorkList").add(obj);
        return {status: 200,message: 'success'};
    }  
    catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
}   
/**
 * 获取企业个人招聘中的工作
 */
const getWork1 = async (req,res) => {
    try {
        const { token } = req
        const { accounts } = verifyToken(token);
        let result = await db.collection('WorkList').where({
            isDel : 0,
            workStatus : 1,
            accounts : accounts
        }).get()
        return {status: 200,message: 'success',data: result};
    }  
    catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
}
/**
 * 获取企业个人已下线的工作
 */
const getWork2 = async (req,res) => {
    try {
        const { token } = req
        const { accounts } = verifyToken(token);
        let result = await db.collection('WorkList').where({
            isDel : 0,
            workStatus : 0,
            accounts : accounts
        }).get()
        return {status: 200,message: 'success',data: result};
    }  
    catch (e) {
        return {status: 400,message: 'FAIL',err: e}
    }
}

module.exports = {
    businessRegister: businessRegister,
    businessLogin: businessLogin,
    changeBusinessUserInfo: changeBusinessUserInfo,
    addWork: addWork,
    getWork1: getWork1,
    getWork2: getWork2

}